$(document).ready(function() {
    $('#btnConnect').click(function(){
        connect(); 
    });
    
    
     
});



function connect(){
    ajaxLoader(true);
    $.ajax({
        url: 'library/Control.php',
        type: "POST",
        data: $('#frmData').serialize()+'&opt=connect',
        success: function(data) {
            ajaxLoader(false);
             var result = JSON.parse(data);
            if(result.type){
                createObjectDialog(result.msg);
            }else{
                createErrorDialog(result.msg)
            }
        },
        error: function(data){
          alert(data);  
        } 
            
    }); 
}

function ajaxLoader(command){
    if(command == true){
        $( "#ajax-loader" ).dialog();
        
    }else{
        $( "#ajax-loader" ).dialog('close');
    }
    
}

function createErrorDialog(data){
    $( "#dialog-error" ).html(data);
    $( "#dialog-error" ).dialog({
        type: true,
        width: 559,
        height: 254,
        resizable: false,
        buttons: [{
            text: "Ok",
            "id": "btnCancel",
            click: function () {
                $( this ).dialog( "close" );
            }

        }]
    });
    
}
function createErrorDialogObject(){
    $( "#dialog-error-object" ).dialog({
        type: true,
        width: 559,
        height: 254,
        resizable: false,
        buttons: [{
            text: "Ok",
            "id": "btnCancel",
            click: function () {
                $( this ).dialog( "close" );
            }

        }]
    });
    
}

function createObjectDialog(data){
    var btnCancel = $("#btnCancel").val();
    var btnConfirm = $("#btnConfirm").val();
    
    $( "#dialog" ).html(data);
    $( "#dialog" ).dialog({
        height: 300,
        width: 1031,
        modal: true,
        buttons: [{
            text: btnCancel,
            "id": "btnCancel",
            click: function () {
                $( this ).dialog( "close" );
            }

        }, {
            text: btnConfirm,
            "id": "btnConfirm",
            click: function () {
                createFiles();
            }
        }]
    });
    _prepareCheckBox();
        
}

function createFiles(){
    if($('#noneTables').is(':checked')){
        createErrorDialogObject();
    }else{
        ajaxLoader(true);
        $.ajax({
            url: 'library/Control.php',
            type: "POST",
            data: $('#frmData').serialize()+'&'+$('#frmTables').serialize()+'&opt=create',
            success: function(data) {
                var result = JSON.parse(data);
                ajaxLoader(false);
                $( "#dialog" ).dialog({
                    height: 634
                });
                $('#idResults').html(result.xhtml);
                $('#idResults').hide('clip');
                $('#idResults').show('clip');
                $('#spanResultLink').click(function(){
                    createZip(result.files);
                });
            }
            
        });
    }
}

function createZip(array){
    ajaxLoader(true);
    var params = "";
    var i = 0;
    $.each(array, function(index, value){
        params += "table["+i+"]="+value+"&"; 
        i++;
    });
    $.ajax({
        url: 'library/Control.php',
        type: "POST",
        data: params+'opt=zip&project='+$('#project').val(),
        success: function(data) {
            ajaxLoader(false);
            openZip(data);         
        } 
    
    });
}

function openZip(data){
    window.open(data); 
}

function _prepareCheckBox(){
    $('#allTables').click(function(){
        if($('#allTables').is(':checked')){
            _selectAllTables();
        }
        
    });
    $('#noneTables').click(function(){
        if($('#noneTables').is(':checked')){
            _deselectAllTables();
        }
    });
    var allTablesSelected = $('#selectTables tr td input[type=checkbox]').not('#noneTables').not('#allTables');
    
    allTablesSelected.click(function(){
        var checked = 0;
        var tables = 0;    
        $('#noneTables').attr('checked',false);
        $('#allTables').attr('checked',false);
        allTablesSelected.each(function() {
            if($(this).is(':checked')){
                checked++;
            }
            tables++;
        });
       
        if(checked == tables){
            $('#allTables').attr('checked',true);
        }
    });
}

function _selectAllTables(){
    $('#selectTables tr td input[type=checkbox]').not('#noneTables').attr('checked',true);
    $('#noneTables').attr('checked',false);
}

function _deselectAllTables(){
    $('#selectTables tr td input[type=checkbox]').not('#noneTables').attr('checked',false);
}

function showFileContent(file){
    var project = $('#project').val();
    ajaxLoader(true);
    $.ajax({
        url: 'library/Control.php',
        type: "POST",
        data: 'file='+file+'&opt=read&project='+project,
        success: function(data) {
            ajaxLoader(false);
            $("#dialogContent").html(data);
            $("#dialogContent").dialog({
                title: file,
                width: 634, 
                height: 200
            } );
                              
            
        }
            
    });
   
}

